﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Entities;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using Common.Utilities;
using OnlineVideos.View_Models;
using Microsoft.Phone.Controls;
using OnlineVideos.UI;

namespace OnlineVideos
{
    public partial class DownloadedImages : UserControl
    {
        #region GlobalDeclaration
        private static bool IsDataLoaded;
        List<ShowLinks> ShowimgList = null;
        ShowDetail detailModel;

        #endregion
        public DownloadedImages()
        {
            InitializeComponent();
            ShowimgList = new List<ShowLinks>();
            IsDataLoaded = false;
        }

        private void lbximgs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                AppSettings.ShowID = (lbximgs.SelectedItem as ShowLinks).ShowID.ToString();
                AppSettings.LinkId = (lbximgs.SelectedItem as ShowLinks).LinkID.ToString();
                PageHelper.NavigateToDownloadedImagePage(AppResources.DownloadImagePageName);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbximgs_SelectionChanged event In downloadedimages file.", ex);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                MyProgressBar1.IsIndeterminate = true;
                detailModel = new ShowDetail();
                GetPageDataInBackground();
                IsDataLoaded = true;

            }
        }
        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {
                                            a.Result = OnlineShow.GetDownloadedImages(AppSettings.ShowID, LinkType.Images/*, false*/);
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {

                                            ShowimgList = (List<ShowLinks>)a.Result;
                                            if (ShowimgList.Count > 0)
                                            {
                                                lbximgs.ItemsSource = ShowimgList;
                                                tblkimgs.Visibility = Visibility.Collapsed;

                                                lbximgs.Loaded += new RoutedEventHandler(UserControl_Loaded);
                                            }
                                            else
                                            {
                                                tblkimgs.Text = "No images  available";
                                                tblkimgs.Visibility = Visibility.Visible;
                                            }
                                            MyProgressBar1.IsIndeterminate = false;
                                        }
                                      );

            bwHelper.RunBackgroundWorkers();
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            ShowDetail detailModel = new ShowDetail();
            detailModel.DeleteDownloadedShowlinks(lbximgs, sender as MenuItem, LinkType.Images);
            GetPageDataInBackground();
        }

        private void btnpintostart_Click(object sender, RoutedEventArgs e)
        {
            string showLinkTitle;
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
            Page p = frame.Content as Page;
            Pivot p1 = (Pivot)(p.FindName("pvtMainDetails"));
            if (p1 != null)
                Constants.topsongnavigation = p1.SelectedIndex.ToString();
            ListBoxItem selectedListBoxItem = this.lbximgs.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            if (selectedListBoxItem == null)
                return;
            showLinkTitle = (selectedListBoxItem.Content as ShowLinks).Title;
            AppSettings.ShowLinkTitle = AppSettings.ShowID + (selectedListBoxItem.Content as ShowLinks).Title;
            ShellTileHelper.PinVideoLinkToStartScreen(AppSettings.ShowID, selectedListBoxItem.Content as ShowLinks);
        }

        private void btnimagFavSong_Click(object sender, RoutedEventArgs e)
        {
            detailModel.AddToFavorites(lbximgs, sender as MenuItem, LinkType.Images);
        }

        private void imgsharelink_Click(object sender, RoutedEventArgs e)
        {
            detailModel.PostAppLinkToSocialNetworks(lbximgs, sender as MenuItem, "  ");
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu mainmenu = sender as ContextMenu;
            foreach (MenuItem contextMenuItem in mainmenu.Items)
            {
                ListBoxItem selectedListBoxItem = this.lbximgs.ItemContainerGenerator.ContainerFromItem((sender as ContextMenu).DataContext) as ListBoxItem;
                if (selectedListBoxItem == null)
                    return;
                if (contextMenuItem.Name == "Pin")
                {
                    if (ShellTileHelper.IsPinned(AppSettings.ShowID + (selectedListBoxItem.Content as ShowLinks).Title))
                    {
                        contextMenuItem.Header = Constants.UnpinFromStartScreen;
                    }
                    else
                    {
                        contextMenuItem.Header = Constants.PinToStartScreen;
                    }
                }
            }
        }
    }
}
